task MigicAbsorb
{

let ShortCount=0;
let LongCount=0;
loop
{
	ascent(let enemy in EnumEnemyBegin .. EnumEnemyEnd)
	{
		let enemyID = EnumEnemyGetID(enemy);
		if(((GetEnemyInfo(enemyID,ENEMY_X)-GetPlayerX)^2+(GetEnemyInfo(enemyID,ENEMY_Y)-GetPlayerY)^2)^0.5<=MagicAbsorbShortRange)
		{
			if(enemyID==0 && GetEnemyLife==0)
			{
			}
			else if(GetEnemyInfo(enemyID,ENEMY_X)>GetClipMinX && GetEnemyInfo(enemyID,ENEMY_X)<GetClipMaxX && GetEnemyInfo(enemyID,ENEMY_Y)>GetClipMinY && GetEnemyInfo(enemyID,ENEMY_Y)<GetClipMaxY)
			{
				if(ShortCount>=5)
				{
				CreateMigicItem(GetEnemyInfo(enemyID,ENEMY_X),GetEnemyInfo(enemyID,ENEMY_Y),1);
				ShortCount=0;
				}
			ShortCount+=1;
			}
		}
		else if(((GetEnemyInfo(enemyID,ENEMY_X)-GetPlayerX)^2+(GetEnemyInfo(enemyID,ENEMY_Y)-GetPlayerY)^2)^0.5<=MagicAbsorbLongRange)
		{
			if(enemyID==0 && GetEnemyLife==0)
			{
			}
			else if(GetEnemyInfo(enemyID,ENEMY_X)>GetClipMinX && GetEnemyInfo(enemyID,ENEMY_X)<GetClipMaxX && GetEnemyInfo(enemyID,ENEMY_Y)>GetClipMinY && GetEnemyInfo(enemyID,ENEMY_Y)<GetClipMaxY)
			{
				if(LongCount>=5)
				{
				CreateMigicItem(GetEnemyInfo(enemyID,ENEMY_X),GetEnemyInfo(enemyID,ENEMY_Y),2);
				LongCount=0;
				}
			LongCount+=1;
			}
		}
	}

yield;
}

}

task CreateMigicItem(x,y,type)
{
		let obj=Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj, imgItem);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj, ADD); 
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexXY(obj, 0, -12, -12);
		ObjEffect_SetVertexXY(obj, 1, 12,  -12);
		ObjEffect_SetVertexXY(obj, 2, 12, 12);
		ObjEffect_SetVertexXY(obj, 3,  -12,  12);
		
		ObjEffect_SetVertexUV(obj, 0,  0,  0);
		ObjEffect_SetVertexUV(obj, 1,  64, 0);
		ObjEffect_SetVertexUV(obj, 2, 64,  64);
		ObjEffect_SetVertexUV(obj, 3, 0, 64);
		ObjEffect_SetScale(obj,1.2,1.2);

			ascent(let i in 0..4)
			{
				ObjEffect_SetVertexColor(obj,i,155,255,255,255);
			}	

		ObjEffect_SetLayer(obj,3);
		Obj_SetPosition(obj,x,y);
		let StandardSpeed=rand(4,9);
		let speed=StandardSpeed;
		//let angle=rand(0,360);
		let angle=atan2(GetPlayerY-y,GetPlayerX-x)+180+rand(-120,120);

		let lightangle=rand(0,360);
		let Scale;
		let DiScale;
		let R;
		let G;
		let B;
		let Alpha=155;
		let VAlpha=50;
		let PowerPlus;
		let CounterPlus;
		let CounterPlusOnBurst;
		let ScorePlus;
		let FragmentPlus=1;
		let amp=1;
		let DifficultChargeAmp=1;
		let DifficultCounterAmp=1;
		let DifficultScoreAmp=1;
		let DifficultFragmentAmp=1;


		alternative(GetCommonDataDefault("SELECTEDDIFFICULT","None"))
		case("Moderate")
		{
			DifficultChargeAmp=1;
			DifficultCounterAmp=1;
			DifficultScoreAmp=1;
			DifficultFragmentAmp=1;
		}
		case("Extream")
		{
			DifficultChargeAmp=1.0;
			DifficultCounterAmp=1.0;
			DifficultScoreAmp=1.5;
			DifficultFragmentAmp=1.0;
		}
		case("Apocalypse")
		{
			DifficultChargeAmp=1.0;
			DifficultCounterAmp=1.0;
			DifficultScoreAmp=2.0;
			DifficultFragmentAmp=1.0;
		}
		others{}

		if(type==1)//΃ACe
		{
			Scale=1.5;
			DiScale=0.015;
			R=127;
			G=255;
			B=255;
			PowerPlus=5;
			CounterPlus=25;
			CounterPlusOnBurst=25;
			ScorePlus=MagicCounter;
		}
		if(type==2)//ACe
		{
			Scale=1;
			DiScale=0.005;
			R=127;
			G=127;
			B=255;
			PowerPlus=2;
			CounterPlus=5;
			CounterPlusOnBurst=5;
			ScorePlus=MagicCounter;
		}
		if(type==25)//ɏACe
		{
			Scale=0.5;
			DiScale=0.0025;
			R=127;
			G=127;
			B=255;
			PowerPlus=0;
			CounterPlus=0;
			CounterPlusOnBurst=0;
			ScorePlus=MagicCounter;
		}
		if(type==3)//ԃACe
		{
			Scale=2;
			DiScale=0.020;
			R=255;
			G=127;
			B=127;
			PowerPlus=10;
			CounterPlus=10;
			CounterPlusOnBurst=10;
			ScorePlus=MagicCounter;
		}
		if(type==4)//ԃACe
		{
			Alpha=100;
			VAlpha=25;
			Scale=1;
			DiScale=0.010;
			R=255;
			G=127;
			B=127;
			PowerPlus=0.5*DifficultChargeAmp;
			CounterPlus=1.0*DifficultCounterAmp;
			CounterPlusOnBurst=1*DifficultCounterAmp;
			ScorePlus=MagicCounter*DifficultScoreAmp;
			FragmentPlus=1*DifficultFragmentAmp;
		}
		if(type==41)//ԃACe
		{
			Alpha=100;
			VAlpha=25;
			Scale=1.2;
			DiScale=0.012;
			R=255;
			G=127;
			B=127;
			PowerPlus=0.5*DifficultChargeAmp*2;
			CounterPlus=1.0*DifficultCounterAmp*2;
			CounterPlusOnBurst=1*DifficultCounterAmp*2;
			ScorePlus=MagicCounter*DifficultScoreAmp*2;
			FragmentPlus=1*DifficultFragmentAmp*2;
		}
		if(type==42)//ԃACe
		{
			Alpha=100;
			VAlpha=25;
			Scale=1.5;
			DiScale=0.015;
			R=255;
			G=127;
			B=127;
			PowerPlus=0.5*DifficultChargeAmp*4;
			CounterPlus=1.0*DifficultCounterAmp*4;
			CounterPlusOnBurst=1*DifficultCounterAmp*4;
			ScorePlus=MagicCounter*DifficultScoreAmp*4;
			FragmentPlus=1*DifficultFragmentAmp*4;
		}
		if(type==45)//ɏԃACe
		{
			Alpha=100;
			VAlpha=25;
			Scale=0.5;
			DiScale=0.005;
			R=255;
			G=127;
			B=127;
			PowerPlus=0.5*DifficultChargeAmp*0;
			CounterPlus=1.0*DifficultCounterAmp*1;
			CounterPlusOnBurst=1*DifficultCounterAmp*1;
			ScorePlus=MagicCounter*DifficultScoreAmp*0;
			FragmentPlus=1*DifficultFragmentAmp*1;
		}
		if(type==5)//唒ACe
		{
			Scale=2;
			DiScale=0.020;
			R=255;
			G=255;
			B=255;
			PowerPlus=3*DifficultChargeAmp;
			CounterPlus=50*DifficultCounterAmp;
			CounterPlusOnBurst=50*DifficultCounterAmp;
			ScorePlus=MagicCounter*100*DifficultScoreAmp;
		}
		if(type==6)//ACe1
		{
			Alpha=100;
			VAlpha=25;
			Scale=1;
			DiScale=0.010;
			R=255;
			G=255;
			B=255;
			PowerPlus=2.5*DifficultChargeAmp;
			CounterPlus=7*DifficultCounterAmp;
			CounterPlusOnBurst=10*DifficultCounterAmp;
			ScorePlus=MagicCounter*10*DifficultScoreAmp;
			FragmentPlus=1*DifficultFragmentAmp;
		}
		if(type==61)//ACe2
		{
			Alpha=100;
			VAlpha=25;
			Scale=1.2;
			DiScale=0.012;
			R=255;
			G=255;
			B=255;
			PowerPlus=2.5*DifficultChargeAmp*2;
			CounterPlus=7*DifficultCounterAmp*2;
			CounterPlusOnBurst=10*DifficultCounterAmp*2;
			ScorePlus=MagicCounter*10*DifficultScoreAmp*2;
			FragmentPlus=1*DifficultFragmentAmp*2;
		}
		if(type==62)//唒ACe3
		{
			Alpha=100;
			VAlpha=25;
			Scale=1.5;
			DiScale=0.015;
			R=255;
			G=255;
			B=255;
			PowerPlus=2.5*DifficultChargeAmp*4;
			CounterPlus=7*DifficultCounterAmp*4;
			CounterPlusOnBurst=10*DifficultCounterAmp*4;
			ScorePlus=MagicCounter*10*DifficultScoreAmp*4;
			FragmentPlus=1*DifficultFragmentAmp*4;
		}
		if(type==65)//ɏACe3
		{
			Alpha=100;
			VAlpha=25;
			Scale=0.5;
			DiScale=0.005;
			R=255;
			G=255;
			B=255;
			PowerPlus=2.5*DifficultChargeAmp*0;
			CounterPlus=7*DifficultCounterAmp*0;
			CounterPlusOnBurst=10*DifficultCounterAmp*0;
			ScorePlus=MagicCounter*10*DifficultScoreAmp*1;
			FragmentPlus=1*DifficultFragmentAmp*1;
		}
		if(type==66)//ϊ唒ACe3
		{
			Alpha=100;
			VAlpha=25;
			Scale=1.75;
			DiScale=0.0175;
			R=255;
			G=255;
			B=255;
			PowerPlus=2.5*DifficultChargeAmp*6;
			CounterPlus=7*DifficultCounterAmp*6;
			CounterPlusOnBurst=10*DifficultCounterAmp*6;
			ScorePlus=MagicCounter*10*DifficultScoreAmp*6;
			FragmentPlus=1*DifficultFragmentAmp*6;
		}
		if(type==7)//ACe
		{
			Scale=1;
			DiScale=0.010;
			R=160;
			G=32;
			B=240;
			PowerPlus=1.0*0.5;
			CounterPlus=10*0.5;
			CounterPlusOnBurst=0;
			ScorePlus=MagicCounter*0.5;
			FragmentPlus=1*0.5;
		}
		if(type==8)//ACe
		{
			Scale=1;
			DiScale=0.010;
			R=255;
			G=255;
			B=255;
			PowerPlus=2.5*DifficultChargeAmp;
			CounterPlus=7*DifficultCounterAmp;
			CounterPlusOnBurst=10*DifficultCounterAmp;
			ScorePlus=MagicCounter*10*DifficultScoreAmp;
			FragmentPlus=1*DifficultFragmentAmp;
		}
		if(type==9)//ÐACe
		{
			Scale=1;
			DiScale=0.010;
			R=0;
			G=255;
			B=255;
			PowerPlus=1;
			CounterPlus=10;
			CounterPlusOnBurst=10;
			ScorePlus=MagicCounter;
			FragmentPlus=1;
		}
			DiScale=0.00;

		if(type<=3 || type==5 || type==7 || type==8 || type==9)
		{
		loop(5)
		{
			lightangle+=12;
			ascent(let i in 0..4)
			{
				ObjEffect_SetVertexColor(obj,i,Alpha+VAlpha*cos(lightangle),R,G,B);
			}
			Obj_SetPosition(obj,Obj_GetX(obj)+speed*cos(angle),Obj_GetY(obj)+speed*sin(angle));
			speed-=StandardSpeed/15;
			Scale-=DiScale*2;
			ObjEffect_SetScale(obj,Scale,Scale);
			wait(1);
		}
		}
		else
		{
		speed=speed*2;
		StandardSpeed=speed;
		loop(15)
		{
			lightangle+=12;
			ascent(let i in 0..4)
			{
				ObjEffect_SetVertexColor(obj,i,155+50*cos(lightangle),R,G,B);
			}
			Obj_SetPosition(obj,Obj_GetX(obj)+speed*cos(angle),Obj_GetY(obj)+speed*sin(angle));
			speed-=StandardSpeed/15;
			Scale-=DiScale*2;
			ObjEffect_SetScale(obj,Scale,Scale);
			if(((Obj_GetX(obj)-GetPlayerX)^2+(Obj_GetY(obj)-GetPlayerY)^2)^0.5<10)
			{
				if(Power<1000 && BurstMode==0)
				{
					Power+=PowerPlus;
				}
				if(MagicCounter<10000 && BurstMode==0)
				{
					MagicCounter+=CounterPlus;
				}
				if(MagicCounter<10000 && BurstMode==1)
				{
					MagicCounter+=CounterPlusOnBurst;
				}

				AddScore(ScorePlus);
				MagicFragment+=FragmentPlus;
				SectionMagicFragment+=FragmentPlus;
				PlaySE("se\seScore.wav");
				Obj_Delete(obj);
			}
			wait(1);
		}
		}
		while(!Obj_BeDeleted(obj))
		{
			Scale-=DiScale;
			ObjEffect_SetScale(obj,Scale,Scale);
			if(speed<10)
			{
			speed+=0.25;
			}
			lightangle+=12;
			ascent(let i in 0..4)
			{
				ObjEffect_SetVertexColor(obj,i,155+50*cos(lightangle),R,G,B);
			}
			let sangle=atan2(GetPlayerY-Obj_GetY(obj),GetPlayerX-Obj_GetX(obj));
			Obj_SetPosition(obj,Obj_GetX(obj)+speed*cos(sangle),Obj_GetY(obj)+speed*sin(sangle));
			if(((Obj_GetX(obj)-GetPlayerX)^2+(Obj_GetY(obj)-GetPlayerY)^2)^0.5<10)
			{
				if(Power<1000 && BurstMode==0)
				{
					Power+=PowerPlus;
				}
				if(MagicCounter<10000 && BurstMode==0)
				{
					MagicCounter+=CounterPlus;
				}
				if(MagicCounter<10000 && BurstMode==1)
				{
					MagicCounter+=CounterPlusOnBurst;
				}

				AddScore(ScorePlus);
				MagicFragment+=FragmentPlus;
				SectionMagicFragment+=FragmentPlus;
				PlaySE("se\seScore.wav");
				Obj_Delete(obj);
			}
		yield;
		}
}